package cn.hunqing.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hunqing.web.dto.InvitationForm;
import cn.hunqing.web.dto.TemplateSimpleDto;
import cn.hunqing.web.entity.Template;
import cn.hunqing.web.entity.TemplateVoteHistory;
import cn.hunqing.web.mapper.TemplateMapper;
import cn.hunqing.web.mapper.TemplateVoteHistoryMapper;
import cn.hunqing.web.service.TemplateService;

@Service
public class TemplateServiceImpl implements TemplateService {
	private static final Logger LOG = LoggerFactory
			.getLogger(TemplateServiceImpl.class.getName());
	
	@Autowired
	private TemplateMapper templateMapper;

	@Autowired
	private TemplateVoteHistoryMapper templateVoteHistoryMapper;
	
	@Override
	public String getTemplatePath(String templateId) {
		int templateIdInt = Integer.parseInt(templateId);
		Template template = templateMapper.get(templateIdInt);
		if(template != null){
			return template.getPath();
		}
		return null;
	}

	@Override
	public String getTemplatePathByWeddingInvitationId(int weddingInvitationId) {
		Template template = templateMapper.getByWeddingInvitationId(weddingInvitationId);
		if(template != null){
			return template.getPath();
		}
		return null;
	}

	@Override
	public List<TemplateSimpleDto> getTopTemplates() {
		List<Template> templates = templateMapper.getTopTemplates();
		if(templates != null){
			List<TemplateSimpleDto> result = new ArrayList<>();
			templates.forEach(template -> {
				TemplateSimpleDto templateSimpleDto = TemplateSimpleDto.buildFrom(template);
//				templateSimpleDto.setPreview(generateTemplatePreview(templateSimpleDto.getPreview(),null));
				result.add(templateSimpleDto);
			});
			return result;
		}
		return null;
	}

	@Override
	public TemplateSimpleDto getTemplate(String templateId) {
		int templateIdInt = Integer.parseInt(templateId);
		Template template = templateMapper.get(templateIdInt);
		if(template != null){
			TemplateSimpleDto templateSimpleDto = TemplateSimpleDto.buildFrom(template);
//			templateSimpleDto.setPreview(generateTemplatePreview(templateSimpleDto.getPreview(),null));
			return templateSimpleDto;
		}
		return null;
	}
	
	@Override
	public TemplateSimpleDto makeCardPreview(InvitationForm invitationForm) {
		Template template = templateMapper.get(invitationForm.getTemplateId());
		if(template != null){
			TemplateSimpleDto templateSimpleDto = TemplateSimpleDto.buildFrom(template);
//			templateSimpleDto.setPreview(generateTemplatePreview(templateSimpleDto.getPreview(),invitationForm));
			return templateSimpleDto;
		}
		return null;
	}

	@Transactional
	@Override
	public void vote(String templateId,int userId) {
		int templateIdInt = Integer.parseInt(templateId);
		TemplateVoteHistory templateVoteHistory = templateVoteHistoryMapper.getByTemplateIdAndUserId(templateIdInt, userId);
		if(templateVoteHistory != null){
			LOG.info("You have voted before!");
			return;
		}
		templateMapper.vote(templateIdInt);
		templateVoteHistory = new TemplateVoteHistory();
		templateVoteHistory.setTemplateId(templateIdInt);
		templateVoteHistory.setUserId(userId);
		templateVoteHistory.setVotedAt(new Date());
		templateVoteHistoryMapper.insert(templateVoteHistory);
	}

}
